define(['jquery', 'bootstrap', 'backend', 'table', 'form','editable'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'express/package/index?batch_id=' + Config.batch_id,
                    add_url: 'express/package/add',
                    edit_url: 'express/package/edit',
                    del_url: 'express/package/del',
                    multi_url: 'express/package/multi',
                    import_url: 'express/package/import',
                    table: 'express_package',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'batch.batch_no',
                            title: __('Batch_id'),
                            operate: 'LIKE',
                            formatter: function (value, row, index) {
                                return row.batch ? value : '-';
                            }
                        },
                        // {field: 'package_no', title: __('Package_no')},
                         {
                            field: 'user_id',
                            title: "用户ID"
                        },
                        {
                            field: 'user.nickname',
                            title: __('User_id'),
                            operate: 'LIKE',
                            formatter: function (value, row, index) {
                                return row.user ? value : '-';
                            }
                        },
                        {field: 'from_express_no', title: __('From_express_no'), operate: 'LIKE'},
                        {field: 'express_no', title: __('Express_no'),editable: true, operate: 'LIKE'},
                        {field: 'express_name', title: __('Express_name'),editable: true, operate: 'LIKE'},
                        {field: 'to_mobile', title: __('To_mobile'),editable: true, operate: 'LIKE'},
                        // {field: 'remarks', title: __('Remarks'), operate: 'LIKE'},
                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: {"1": __('Status 1'), "2": __('Status 2'), "3": __('Status 3')},
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'track.contents',
                            title: __('Track_id'),
                            operate: 'LIKE',
                            formatter: function (value, row, index) {
                                return row.track ? value : '-';
                            }
                        },
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },

                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons:[
                                {
                                    name: 'ajax',
                                    text: __('退出批次'),
                                    title: __('退出批次'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-trash',
                                    confirm: '确认退出批次？',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.batch_id > 0){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                    success: function (data, ret) {
                                        // Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                        $(".btn-refresh").trigger("click"); //刷新数据
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                    error: function (data, ret) {
                                        return false;
                                    },

                                    url: function (row) {
                                        return `express/package/delbatch?id=${row.id}`;
                                    },
                                    callback: function (data) {
                                        $(".btn-refresh").trigger("click"); //刷新数据
                                    }
                                },
                            ],
                        }
                    ]
                ]
            });

            $(document).on("click", ".btn-addBatch", function () {
                if ($(this).data('id')) {
                    Backend.api.open('express/package/batch/ids/' + $(this).data('id'), "加入批次", {area: ["860px", "310px"]});
                } else {
                    Backend.api.open('express/package/batch/ids/' + Table.api.selectedids(table), "加入批次", {area: ["860px", "310px"]});
                }
            });


            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {

            Controller.api.bindevent();
        },
        batch: function () {

            $(document).on("click", "input[name='row[type]']", function () {
                var type = $(this).val();
                if (type == 'old') {
                    $('.batch-select').removeClass('hide')
                } else {
                    $('.batch-select').addClass('hide')
                }
            });

            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
